package lumis.portal.page.webresource;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lumis.portal.PortalException;
import lumis.portal.PortalStringResource;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.portal.stability.StableMinor;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.IResource;

/**
 * Default page web resource data provider.
 * 
 * Simply returns the data specified in the page properties.
 *
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 * @since 5.0.0
 */
@StableMinor(version = "6.0", sinceVersion = "5.0")
public class DefaultPageWebResourceDataProvider extends AbstractPageWebResourceDataProvider
{
	@Override
	protected String getTitle() throws PortalException
	{
		// calculate the title for the page web resource
		String title;
		String runTimeServiceInterfaceId = pageWebResource.getRunTimeServiceInterfaceId();
		if (runTimeServiceInterfaceId != null)
		{
			// use the name of the runtime interface
			ServiceInterfaceConfig serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(runTimeServiceInterfaceId, PortalTransactionFactory.getCurrentTransaction());
			title = serviceInterfaceConfig.getName();
			
			// localize the title
			IResource resource = serviceInterfaceConfig.getResource();
			title = ManagerFactory.getLocalizationManager().localize(title, resource, SessionConfig.getCurrentSessionConfig().getLocale());
		}
		else
		{
			// use the page's title
			title = pageWebResource.getPage().getTitle();
			if(title == null)
			{
				// use the page's name
				title  = pageWebResource.getPage().getName();
			}
			
			// localize the title
			title = ManagerFactory.getLocalizationManager().localize(title, new PortalStringResource(), SessionConfig.getCurrentSessionConfig().getLocale());
		}
		
		return title;
	}

	@Override
	protected String getDescription() throws PortalException
	{
		return  ManagerFactory.getLocalizationManager().localize(pageWebResource.getPage().getDescription(), new PortalStringResource(), SessionConfig.getCurrentSessionConfig().getLocale());
	}
	
	@Override
	protected List<String> getKeywords() throws PortalException
	{
		PortalStringResource portalStrings = new PortalStringResource();
		String keywordsStr = pageWebResource.getPage().getKeywords();
		if(keywordsStr != null)
		{
			String[] keywordsArray = keywordsStr.split(",");
			ArrayList<String> keywords = new ArrayList<String>(keywordsArray.length);
			for(String keyword : keywordsArray)
			{
				String curKeyword = ManagerFactory.getLocalizationManager().localize(keyword, portalStrings, SessionConfig.getCurrentSessionConfig().getLocale());
				if(curKeyword != null && curKeyword.length() > 0)
					keywords.add(curKeyword);
			}		
			return keywords;
		}
		else
		{
			return Collections.emptyList();
		}
	}
}
